package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrdersTask {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = "* 0 * * * ?")
    public void processTimeoutOrder(){
       // log.info("定时任务时间{}" , LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);

     List<Orders> list = orderMapper.getByStatusAndOrdertimeLT(Orders.PENDING_PAYMENT,time);

     if(null !=list && list.size()>0){
         for (Orders orders : list) {
             orders.setStatus(Orders.CANCELLED);
             orders.setCancelTime(LocalDateTime.now());
             orders.setCancelReason("超时订单");
             orderMapper.update(orders);
         }

     }

    }
    /**
     * 处理“派送中”状态的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrder(){
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);

        List<Orders> list = orderMapper.getByStatusAndOrdertimeLT(Orders.DELIVERY_IN_PROGRESS, localDateTime);

        if(null!= list && list.size()>0){
            for (Orders orders : list) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }

    }

}
